@{
    ViewBag.Title = "Unicode转中文、中文Unicode编码、ASCII转Unicode、Unicode转ASCII";
    ViewBag.keywords = "Unicode转中文,中文转Unicode,ASCII转Unicode,Unicode转ASCII";
    ViewBag.description = "在线Unicode编码转换工具,Unicode转中文,中文转Unicode,ASCII转Unicode,Unicode转ASCII";
}
<div class="layui-row layui-form layui-form-pane">
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">内容：</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" id="content" name="content" rows="8" onpaste="setTimeout(encode,1)" lay-verify="required" autocomplete="off" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <a class="layui-btn" onclick="decodeUnicode();" style="background-color:#0079c1;">Unicode转中文</a>
        <a class="layui-btn" onclick="encodeUnicode();" style="background-color:#49176d;">中文Unicode编码</a>
        <a class="layui-btn" onclick="asciiToUnicode();" style="background-color:#00a0af;">ASCII转Unicode</a>
        <a class="layui-btn layui-btn-normal" onclick="unicodeToAscii();" style="background-color:#49a942;">Unicode转ASCII</a>
        <a class="layui-btn layui-btn-danger" onclick="cleanup();">清空</a>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">输出：</label>
        <div class="layui-input-block">
            <textarea placeholder="输出内容" id="outContent" name="outContent" rows="8" onpaste="setTimeout(decode,1)" autocomplete="off" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <div><a></a></div>
    </div>
</div>

@section Scripts{
<script>
    let area_input = document.getElementById('content');
    let area_output = document.getElementById('outContent');
    function decodeUnicode() {
        let input = area_input.value;
        area_output.value = unescape(input.replace(/\\u/gi, '%u'));
        selectBtn(0)
    }
    function encodeUnicode() {
        let input = area_input.value; let res = [];
        for (let i = 0; i < input.length; i++) {
            res[i] = ("00" + input.charCodeAt(i).toString(16)).slice(-4);
        }
        area_output.value = "\\u" + res.join("\\u");
        selectBtn(1)
    }
    function asciiToUnicode() {
        let input = area_input.value;
        let code = input.match(/&#(\d+);/g);
        let result = '';
        if (code === null) {
            area_output.value = '';
            return
        }
        for (let i = 0; i < code.length; i++) {
            result += String.fromCharCode(code[i].replace(/[&#;]/g, ''));
        }
        area_output.value = result;
        selectBtn(2)
    }
    function unicodeToAscii() {
        let input = area_input.value;
        let result = '';
        for (let i = 0; i < input.length; i++) {
            result += '&#' + input.charCodeAt(i) + ';';
        }
        area_output.value = result;
        selectBtn(3)
    }
    function selectBtn(index) {
        let btns = document.querySelectorAll('#btns button');
        for (let i = 0; i < btns.length; i++) {
            if (i === index) {
                btns[i].classList.add('primary')
            }
            else {
                btns[i].classList.remove('primary')
            }
        }
    }
    function cleanup() {
        area_input.value = '';
        area_output.value = '';
        selectBtn(-1)
    }
</script>
}
